package com.xgry.bpm.api.business.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.business.model.dao.ModelDao;
import com.xgry.bpm.api.business.model.entity.Model;
import com.xgry.bpm.api.business.model.form.ModelForm;
import com.xgry.bpm.api.business.model.service.ModelService;
import com.xgry.bpm.api.business.model.vo.ModelVO;
import org.apache.xpath.operations.Mod;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * Model服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@Service
public class ModelServiceImpl extends ServiceImpl<ModelDao, Model> implements ModelService {

    @Override
    public int save(ModelForm record) {
        Model data = this.convertDO(record);
        int insert = baseMapper.insert(data);
        record.setId(data.getId());
        return insert;
    }

    @Override
    public int updateById(ModelForm record) {
        //修改时间
        Model data = this.convertDO(record);
        return baseMapper.updateById(data);
    }

    @Override
    public int deleteById(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public ModelVO selectById(String id) {
        Model data = baseMapper.selectById(id);
        ModelVO vo = new ModelVO();
        BeanUtils.copyProperties(data, vo);
        return vo;
    }

    @Override
    public List<ModelVO> selectAll(ModelForm record) {
        Model data = this.convertDO(record);
        QueryWrapper<Model> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<Model> list = baseMapper.selectList(queryWrapper);
        return this.convert(list);
    }

    @Override
    public IPage<ModelVO> selectPage(ModelForm record) {
        // form -> do 转换
        Model data = this.convertDO(record);
        // 分页数据设置
        Page<Model> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<Model> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<Model> list = baseMapper.selectPage(page, queryWrapper);
        IPage<ModelVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }

    @Override
    public IPage<Model> getPage(IPage<Model> page, QueryWrapper<Model> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<ModelVO> findByModelClassId(Integer modelClassId) {
        List<Model> list = baseMapper.selectList(new QueryWrapper<Model>() {{
            this.eq("modelClassId", modelClassId);
        }});
        return this.convert(list);
    }

    @Override
    public List<ModelVO> findByModelCode(Integer code) {
        List<Model> list = baseMapper.selectList(new QueryWrapper<Model>() {{
            this.eq("code", code);
        }});
        return this.convert(list);
    }

    @Override
    public List<ModelVO> findByModelName(String modelName) {
        List<Model> list = baseMapper.selectList(new QueryWrapper<Model>() {{
            this.eq("modelName", modelName);
        }});
        return this.convert(list);
    }


    /**
     * Form -> Do
     *
     * @param form 对象
     * @return Do对象
     */
    private Model convertDO(ModelForm form) {
        Model data = new Model();
        BeanUtils.copyProperties(form, data);
        return data;
    }

    /**
     * Do -> VO
     *
     * @param list 对象
     * @return VO对象
     */
    private List<ModelVO> convert(List<Model> list) {
        List<ModelVO> modelList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return modelList;
        }
        for (Model source : list) {
            ModelVO target = new ModelVO();
            BeanUtils.copyProperties(source, target);
            modelList.add(target);
        }
        return modelList;
    }

}
